﻿//---------------------------------------------------------------------------------
// Copyright 2011 Microsoft Corporation
// Licensed under the Microsoft Public License (MS-PL) (the "License"); 
// You may not use this file except in compliance with the License. 
// You may obtain a copy of the License at http://www.opensource.org/licenses/ms-pl.html
// THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR 
// CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, 
// INCLUDING WITHOUT LIMITATION ANY IMPLIED WARRANTIES OR 
// CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, 
// MERCHANTABLITY OR NON-INFRINGEMENT. 
// See the Microsoft Public License (MS-PL) for the specific language governing 
// permissions and limitations under the License.
//---------------------------------------------------------------------------------

using System.Collections.Generic;
using System.Windows;
using System.Windows.Media;

namespace Microsoft.Telco.Controls
{
    /// <summary>
    /// 
    /// </summary>
    public static class VisualTreeExtensions
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="depObject"></param>
        /// <returns></returns>
        public static IEnumerable<DependencyObject> GetChildren(this DependencyObject depObject)
        {
            int count = depObject.GetChildrenCount();
            for (int i = 0; i < count; i++)
            {
                yield return VisualTreeHelper.GetChild(depObject, i);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="depObject"></param>
        /// <param name="childIndex"></param>
        /// <returns></returns>
        public static DependencyObject GetChild(this DependencyObject depObject, int childIndex)
        {
            return VisualTreeHelper.GetChild(depObject, childIndex);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="depObject"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public static DependencyObject GetChild(this DependencyObject depObject, string name)
        {
            return depObject.GetChild(name, false);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="depObject"></param>
        /// <param name="name"></param>
        /// <param name="recursive"></param>
        /// <returns></returns>
        public static DependencyObject GetChild(this DependencyObject depObject, string name, bool recursive)
        {
            foreach (var child in depObject.GetChildren())
            {
                var element = child as FrameworkElement;
                if (element != null)
                {
                    if (element.Name == name)                                           // If its a FrameworkElement check Name
                        return element;
                    var innerElement = element.FindName(name) as DependencyObject;      // Try to get it using FindByName might be more efficient
                    if (innerElement != null)
                        return innerElement;
                }
                if (recursive)                                                          // If it's recursive search through its children
                {
                    var innerChild = child.GetChild(name, true);
                    if (innerChild != null)
                        return innerChild;
                }
            }
            return null;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="depObject"></param>
        /// <returns></returns>
        public static int GetChildrenCount(this DependencyObject depObject)
        {
            return VisualTreeHelper.GetChildrenCount(depObject);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="depObject"></param>
        /// <returns></returns>
        public static DependencyObject GetParent(this DependencyObject depObject)
        {
            return VisualTreeHelper.GetParent(depObject);
        }
    }
}
